package com.carlwang.dao.imp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.carlwang.dao.BasicDao;
import com.carlwang.pojo.User;

public class UserDaoImp extends BasicDao<User, List> {

	@Override
	public boolean create(User pT) {

		Session session = getSessionFactory().openSession();
		Transaction tr = session.beginTransaction();
		session.save(pT);
		tr.commit();
		session.close();
		getSessionFactory().close();
		return true;

	}

	@Override
	public boolean update(User pT) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(User pT) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List select(User pT) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean login(User pUser) {
		Session session = getSessionFactory().openSession();
		Transaction tr = session.beginTransaction();
		String hql = "FROM User WHERE userName = ? AND password = ?";
		List<User> list = session.createQuery(hql)
				.setString(0, pUser.getUserName())
				.setString(1, pUser.getPassword()).list();

		tr.commit();
		session.close();
		getSessionFactory().close();
		if (list.size() > 0) {
			return true;
		}
		return false;

	}

}
